(ns eventsmashup.templates
  (:use [net.cgrand.enlive-html :as html] 
        [net.cgrand.moustache :only [app]]
        [eventsmashup.utils :only [run-server render-to-response page-not-found render]]
        [eventsmashup.data]
  [clojure.pprint]))

(def table-template (html/html-resource "eventsmashup/index.html"))

(def *first-cell-sel* [[:tr (html/nth-child 1)][:td html/first-child]])

(def *rest-cell-sel* [[:table] [:tr (html/nth-child 2)][:td (html/nth-child 2)]])

(def *row-sel* [[:table] [:tr html/first-child]])

(def *header-cell-sell* [[:thead] [:tr first-child] [:th first-child]])


(defsnippet second-cell "eventsmashup/index.html"
  [[:table] [:tr (html/nth-child 1)][:td (html/nth-child 2)]]
  [{value :performer}]
  [:td] (html/content value))

(defsnippet cell-model "eventsmashup/index.html" *first-cell-sel*
  [{value :event-name}]
  [:td] (html/content value))

(defsnippet test-snippet "eventsmashup/index.html" [[:tr (has [:td])]] [{:keys[event-name performer date start-time end-time]}]
  
  [[:td (nth-child 1)]] (content event-name)
  [[:td (nth-child 2)]] (content performer)
  [[:td (nth-child 3)]] (content date)
  [[:td (nth-child 4)]] (content start-time)
  [[:td (nth-child 5)]] (content end-time))

(defsnippet row-model "eventsmashup/index.html" *row-sel* 
  [{:keys [event-data]} model]
  [:tr] (html/content (map model event-data)))

(deftemplate index "eventsmashup/index.html"
  [{:keys [event-data]}]
  ;[:tbody] (html/content (map #(row-model % cell-model % second-cell)event-data)))
  [:tbody] (html/content (map #(test-snippet) event-data)))

;--show the page

(def routes
     (app
      [""]  (fn [req] (render-to-response (index *dummy-content*)))
      [&]   page-not-found))

;; ========================================
;; The App
;; ========================================

(defonce *server* (run-server routes))



